package ontology;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;
import org.jdom.Element;


public class Utils {

    public static Polarity getPolarity(Element interaction) {
        if (interaction.getAttributeValue("polarity") != null) {
            return Polarity.getPolarity(interaction.getAttributeValue("polarity"));
        } else {
            return Polarity.NULL;
        }
    }

    public static Control getControl(Element interaction) {
        if (interaction.getAttributeValue("control") != null) {
            return Control.getControl(interaction.getAttributeValue("control"));
        } else {
            return Control.NULL;
        }
    }

    public static int informative(Element a, Element b) {
        if (a.getAttributeValue("id").equals("interacts with") &&
                b.getAttributeValue("id").equals("interacts with")) {
            return 0;
        }
        if (a.getAttributeValue("id").equals("interacts with")) {
            return 1;
        }
        if (b.getAttributeValue("id").equals("interacts with")) {
            return 2;
        }
        return 3;
    }

    public static int couldBeSameAction(Element a, Element b) {

        int informative = informative(a, b);
        if (informative != 3) {
            if (informative == 0) {
                return 3;
            } else if (informative == 1) {
                return 1;
            } else {
                return 2;
            }
        }


        if (opposing(a, b)) {
            return 0;
        }

        if (a.isAncestor(b)) {
            return 1;
        }
        if (b.isAncestor(a)) {
            return 2;
        } else if (a.getParentElement().equals(b.getParentElement())) {
            return 3;
        }

        return 0;
    }

    private static boolean opposing(Element a, Element b) {
        boolean print = false;
//        System.out.println(a.getAttributeValue("id")+", "+b.getAttributeValue("id"));
//        if(a.getAttributeValue("id").equals("stabilizes") || b.getAttributeValue("id").equals("stabilizes")){
//            print = true;
//        }
        Direction aDir = getDirection(a);
        Direction bDir = getDirection(b);
        Polarity aPol = getPolarity(a);
        Polarity bPol = getPolarity(b);
        Control aCon = getControl(a);
        Control bCon = getControl(b);
//        if(print){
//            System.out.println(aDir.name()+", "+bDir.name());
//            System.out.println(aPol.name()+", "+bPol.name());
//            System.out.println(aCon.name()+", "+bCon.name());
//        }
        if (aDir.compareWith(bDir).equals(Comparison.HAS_OPPOSING)) {
            return true;
        }
        if (aPol.compareWith(bPol).equals(Comparison.HAS_OPPOSING)) {
            return true;
        }
        if (aCon.compareWith(bCon).equals(Comparison.HAS_OPPOSING)) {
            return true;
        }
        return false;

    }

    public static Direction getDirection(Element interaction) {
        if (interaction.getAttributeValue("direction") != null) {
            return Direction.getDirection(interaction.getAttributeValue("direction"));
        } else {
            return Direction.NULL;
        }
    }




}
